package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Attendance;
import com.itheima.pojo.Result;
import com.itheima.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    @PostMapping("/add")
    public Result add(@RequestBody Attendance attendance) {
        attendanceService.save(attendance);
        return Result.success("成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        attendanceService.removeById(id);
        return Result.success("成功");
    }

    @PutMapping("/put/{id}")
    public Result update(@PathVariable Long id, @RequestBody Attendance attendance) {
        // 校验 ID 是否匹配
        if (id == null) {
            return Result.error("请求的ID与数据中ID不一致");
        }

        // 更新操作
        boolean isUpdated = attendanceService.updateById(attendance);

        if (isUpdated) {
            return Result.success("成功");
        } else {
            return Result.error("更新失败");
        }
    }

    // 获取考勤信息
    @GetMapping("/detail/{id}")
    public Result getAttendanceById(@PathVariable Long id) {
        // 根据 ID 查询考勤信息
        Attendance attendance = attendanceService.getById(id);

        // 如果没有找到对应的考勤信息，返回错误信息
        if (attendance == null) {
            return Result.error("未找到该考勤信息");
        }

        // 返回成功的响应，包含考勤信息
        return Result.success(attendance);
    }

    @GetMapping("/list")
    public Result<Page> page(Integer page, Integer pagesize,String keyword, Integer status) {
        Page page1 = new Page(page, pagesize);
        return attendanceService.selectPage(page1, keyword, status);
    }

}
